package com.zegoto.mail.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Service
public class ThreadTask {

    private static final Logger LOGGER = LoggerFactory.getLogger(ThreadTask.class);

    public void exec() {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 200, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(5));
        LOGGER.info("线程池实例化。。。。");

        for(int i=0;i<15;i++){
            MyTask myTask = new MyTask();
            myTask.setCount(i);
            executor.execute(myTask);
            System.out.println("线程池中线程数目："+executor.getPoolSize()+"，队列中等待执行的任务数目："+
                    executor.getQueue().size()+"，已执行玩别的任务数目："+executor.getCompletedTaskCount());
        }
        executor.shutdown();
    }

    class MyTask implements Runnable{
        private final Logger LOGGER = LoggerFactory.getLogger(MyTask.class);
        private Integer count;
        public void setCount(Integer count) {
            this.count = count;
        }
        @Override
        public void run() {
            LOGGER.info("thread : {} count: {}", Thread.currentThread().getName(), count);
        }
    }
}
